import { getCookie, setCookie, removeCookie } from '@/utils/jsCookie.ts'
import { TOKEN, USERINFO } from '@/constant'
import { defineStore } from 'pinia'
import { userGetUser } from '@/api/interface/web/user'
export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: getCookie(USERINFO) || null,
    token: getCookie(TOKEN) || null,
    dialogLoginVisible: false,
    hasHandleDialogLoginSubmit: false,
  }),

  actions: {
    async login(userInfo: any) {
      setCookie(USERINFO, userInfo)
      setCookie(TOKEN, userInfo.token)
      this.userInfo = userInfo
      this.token = userInfo.token
    },
    async logout() {
      removeCookie(USERINFO)
      removeCookie(TOKEN)
      this.userInfo = null
      this.token = null
    },
    async refreshUserInfo() {
      const user: any = await userGetUser({})
      setCookie(USERINFO, user)
      this.userInfo = user
    },
    //登录弹框显示隐藏
    async setDialogLoginVisible(visible: any) {
      this.dialogLoginVisible = visible
    },
    //确认登录
    async setHasHandleDialogLoginSubmit() {
      this.hasHandleDialogLoginSubmit = !this.hasHandleDialogLoginSubmit
    },
  },
})
